package com.leetcode;

/**
 * @Classname Leetcode714
 * @Description TODO
 * @Date 2021-08-07 19:35
 * @Created by darrren
 */
public class Leetcode714 {
    /*
    //solution 1:参考Leetcode122
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int [][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for(int i=1; i<n; i++){
            //买入股票不需要收费，卖出股票需要收费
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] + prices[i]-fee);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] - prices[i]);
        }


        return dp[n-1][0];
    }
*/

    //solution 2:
    //根据solution进行优化，
    // dp[i][0] 代表没有股票，也就是卖出，用sell代替
    // dp[i][0] 代买持有股票，也就是买入，用buy代替
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int sell = 0;
        int buy = -prices[0];

        for(int i=1; i<n; i++){
            //买入股票不需要收费，卖出股票需要收费
            sell = Math.max(sell,buy + prices[i]-fee);
            buy = Math.max(buy,sell - prices[i]);
        }


        return sell;
    }

//[1,3,7,5,10,3]
//3

    public static void main(String[] args){
        int[] prices = {1, 3, 7, 5, 10, 3};
        Leetcode714 leetcode = new Leetcode714();
        System.out.println(leetcode.maxProfit(prices, 3));
    }
}
